import math as m
import os
import numpy as np
from matplotlib import pyplot as plt


def SFHM(times, a, b, x_value, y_value):
    '''
    论文
    Lai, Q., Liu, Y., 2023.
    A cross-channel color image encryption algorithm using two-dimensional hyperchaotic map.
    Expert Systems with Applications 223, 119923. https://doi.org/10.1016/j.eswa.2023.119923

    SFHM 二维改进超混沌系统：
    输出结果在experiment文件夹下sfhm_x.txt, sfhm_y.txt
    1. times 是循环次数
    2. a,b 是sfhm的控制参数, a, b控制范围【0，100】
    3. x_value, y_value 是初始值
    '''
    _file_x = open(r'./sfhm_x.txt', 'w+')
    _file_y = open(r'./sfhm_y.txt', 'w+')

    _x = [0] * times
    _y = [0] * times
    _x[0] = x_value
    _y[0] = y_value

    for index in range(0, times - 1):
        _x[index + 1] = m.sin(a * m.pi * m.pi / (_x[index] * _y[index]))
        _y[index + 1] = m.sin(b * m.pi * m.pi * _x[index] * (1 - _y[index]))

    print(_x)
    print(_y)

    _file_x.write(str(_file_x))
    _file_y.write(str(_file_y))

    _file_x.close()
    _file_y.close()


def Logistic(u=4, initvalue=0.32, times=100):
    '''
    :param u: Logistics的控制参数
    :param initvalue: 初始值
    :param times: 循环次数
    :return: Logistic 当前参数下结果，同时结果也会写到experiment文件夹下
    '''

    file = open(r'..\experiment\Logistics.txt', 'w+')  # 文件保存路径
    # 申请_xlist结果
    _xlist = [0] * times
    _xlist[0] = initvalue

    for index in range(0, times - 1):
        _xlist[index + 1] = u * _xlist[index] * (1 - _xlist[index])

    # print(_xlist)
    for line in _xlist:
        file.write(str(line)+'\n')
    file.close()
    return _xlist

def Sine(initValue, w, times):
    '''
    1-D Sine map
    :param initValue:
    :param w:
    :param times:
    :return:
    '''
    x = np.zeros(times+1)
    x[0] = initValue
    for i in range(times):
        x[i+1] = w * np.sin(np.pi * x[i])

    return x

def _2D_sine_logistic(u,w,init_x,init_y,times):
    '''
    :param times: 循环次数
    :param u: 系统控制参数 [0,4]
    :param w: 系统控制参数 大于等于2
    :param init_x: 系统初始值
    :param init_y: 系统初始值
    :return: 2Dsine_Logistic迭代结果
    '''
    # 申请x，y
    x = np.zeros(times)
    y = np.zeros(times)

    #赋值初始值
    x[0] = init_x
    y[0] = init_y
    for n in range(1, times):
        x[n] = u * np.sin(np.pi*np.pi * y[n-1]/(1-x[n-1]))
        y[n] = w * np.sin(np.pi*np.pi / (x[n-1]*y[n-1]))

    return x[1:], y[1:]


def Henon(x,y,n):
    for i in range(n):
        x1 = 1 - 1.4 * x ** 2 + y
        y1 = 0.3 * x
        x = x1
        y = y1
    return x,y








